package com.qxzn.module.mine.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.qxzn.app.api.api.HttpRequest;
import com.qxzn.app.bean.BaseBean;
import com.qxzn.app.bean.FamilyMemberBean;
import com.qxzn.app.page.BaseActivity;
import com.qxzn.app.page.interfaces.MyInterface;
import com.qxzn.app.page.model.MineModel;
import com.qxzn.app.page.widget.ToastUtil;
import com.qxzn.app.page.widget.statusbar.StatusBar;
import com.qxzn.app.util.AppUtil;
import com.qxzn.app.util.CommonUtil;
import com.qxzn.app.util.SharePreferenceUtil;
import com.qxzn.module.mine.R;
import com.qxzn.module.mine.R2;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 查看待加入家庭圈用户
 */
public class ViewToJoinFamilyActivity extends BaseActivity implements MyInterface.AuthMemberListener {
    @BindView(R2.id.iv_head)
    ImageView ivHead;
    @BindView(R2.id.tv_name)
    TextView tvName;
    @BindView(R2.id.tv_phone)
    TextView tvPhone;
    @BindView(R2.id.tv_confirm)
    TextView tvConfirm;
    @BindView(R2.id.tv_notice)
    TextView tvNotice;
    private MineModel mineModel;
    private FamilyMemberBean.MemberBean memberBean;

    @Override
    protected int initLayout() {
        return R.layout.activity_view_to_join_family;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void iniTitleBar() {
        title_bar.setVisibility(View.GONE);
    }

    @Override
    protected void setStatusBarColor() {
        StatusBar.setStatusBarLightMode(this, getResources().getColor(R.color.blue_3FC7FF));
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mineModel = new MineModel(this);
        mineModel.setAuthMemberListener(this);
        memberBean = (FamilyMemberBean.MemberBean) getIntent().getSerializableExtra("member_bean");
        if (memberBean == null) {
            AppUtil.showToast(this, "数据有误");
            finish();
            return;
        }
        Glide.with(this).load(HttpRequest.BASE_URL + memberBean.getHeadportrait()).apply(RequestOptions.circleCropTransform().placeholder(R.mipmap.icon_xiaopai).error(R.mipmap.icon_xiaopai)).into(ivHead);
        tvName.setText(memberBean.getNiname());
        tvPhone.setText(memberBean.getUsername());
        if ("-1".equals(memberBean.getRoles())) {
            tvConfirm.setVisibility(View.VISIBLE);
        } else {
            tvConfirm.setVisibility(View.GONE);
            if ("-2".equals(memberBean.getRoles())) {
                tvNotice.setText("对方同意后，才会加入家庭圈哦");
            } else if ("-4".equals(memberBean.getRoles())) {
                tvNotice.setText("对方已拒绝您的邀请");
            }
        }
    }

    @OnClick({R2.id.iv_back1, R2.id.tv_confirm})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.iv_back1) {
            finish();
        } else if (i == R.id.tv_confirm) {
            if (CommonUtil.isFastClick()) {
                return;
            }
            showProgress();
            JSONObject params = new JSONObject();
            params.put("device_id", SharePreferenceUtil.getDeviceId(this));
            params.put("username", memberBean.getUsername());
            params.put("roles", 1);
            mineModel.authMember(params);
        }
    }

    @Override
    public void onAuthMemberSuccess(BaseBean baseBean) {
        hideProgress();
        if (baseBean != null) {
            if (baseBean.getCode() == 200) {
                ToastUtil.showImgToast(ViewToJoinFamilyActivity.this, "已通过", R.mipmap.icon_success);
                setResult(RESULT_OK);
                finish();
                return;
            }
        }
        if (baseBean != null && !TextUtils.isEmpty(baseBean.getMsg())) {
            ToastUtil.showImgToast(ViewToJoinFamilyActivity.this, baseBean.getMsg(), R.mipmap.icon_failed);
        } else {
            ToastUtil.showImgToast(ViewToJoinFamilyActivity.this, "邀请失败，请重试", R.mipmap.icon_failed);
        }
    }

    @Override
    public void onAuthMemberFailure() {
        hideProgress();
        AppUtil.showToast(ViewToJoinFamilyActivity.this, "网络错误，请稍后再试！");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mineModel != null) {
            mineModel.removeListener();
        }
    }

}
